package com.yanxinim.cn.store.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yanxinim.cn.R;
import com.yanxinim.cn.store.StoreCreateAddressActivity;
import com.yanxinim.cn.store.bean.StoreAddressInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 店铺地址列表Adapter
 */
public class StoreAddressListAdapter extends RecyclerView.Adapter<StoreAddressListAdapter.ViewHolder> {

    private Context mContext;
    private List<StoreAddressInfo> storeAddressInfos;
    private OnDefaultListener onDefaultListener;
    private ItemOnClick itemOnClick;

    public ItemOnClick getItemOnClick() {
        return itemOnClick;
    }

    public void setItemOnClick(ItemOnClick itemOnClick) {
        this.itemOnClick = itemOnClick;
    }

    public OnDefaultListener getOnDefaultListener() {
        return onDefaultListener;
    }

    public void setOnDefaultListener(OnDefaultListener onDefaultListener) {
        this.onDefaultListener = onDefaultListener;
    }

    public StoreAddressListAdapter(Context mContext, List<StoreAddressInfo> storeAddressInfos) {
        this.mContext = mContext;
        this.storeAddressInfos = storeAddressInfos;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.h_store_address_list_info_layout, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        StoreAddressInfo storeAddressInfo = storeAddressInfos.get(position);
        if (storeAddressInfo.getAddressDefault().equals("1")) {
            viewHolder.tvDefault.setTag("2");
            viewHolder.tvDefault.setBackgroundResource(R.drawable.bg_btn_red);
            viewHolder.tvDefault.setTextColor(mContext.getResources().getColor(R.color.white));
        } else {
            viewHolder.tvDefault.setTag("1");
            viewHolder.tvDefault.setBackgroundResource(R.drawable.a_gray_line_bg);
            viewHolder.tvDefault.setTextColor(mContext.getResources().getColor(R.color.gray));
        }
        viewHolder.userName.setText(storeAddressInfo.getUserName());
        viewHolder.userPhone.setText(storeAddressInfo.getUserPhone());
        String addressText = storeAddressInfo.getAddressText();
        String addressCode = storeAddressInfo.getAddressCode();
        viewHolder.addressInfo.setText(String.format("%s%s",
                addressText,
                addressCode
        ));
        viewHolder.editAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, StoreCreateAddressActivity.class);
                intent.putExtra("titleName", "编辑收货地址");
                intent.putExtra("data", storeAddressInfo);
                mContext.startActivity(intent);
            }
        });

     /*   viewHolder.tvDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewHolder.tvDefault.getTag().equals("1")) {
                    onDefaultListener.onDefaultClick(storeAddressInfo);
                }
            }
        });*/

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemOnClick.itemOnClick(storeAddressInfo);
            }
        });
    }

    public interface ItemOnClick {
        void itemOnClick(StoreAddressInfo storeAddressInfo);
    }

    public interface OnDefaultListener {
        void onDefaultClick(StoreAddressInfo storeAddressInfo);
    }

    @Override
    public int getItemCount() {
        return storeAddressInfos.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.user_name)
        TextView userName;
        @BindView(R.id.user_phone)
        TextView userPhone;
        @BindView(R.id.address_info)
        TextView addressInfo;
        @BindView(R.id.edit_address)
        ImageView editAddress;
        @BindView(R.id.tv_default)
        TextView tvDefault;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
